package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yaml.snakeyaml.events.Event;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 要求跳转到userAjax页面中
     * @return
     */
    @RequestMapping("userAjax")
    public String userAjax(){

        return "userAjax";

    }
    /**
     * 接收ajax请求/findAjaxUser
     * 返回值结果 ：JSON字符串
     *
     */
    @RequestMapping("/findAjaxUser")
    @ResponseBody
    public List<User> findAjax(){

        return userService.findAll();
    }







    @RequestMapping("/demo")
    @ResponseBody
    public String demo(){

        return "框架整合初步完成";
    }
    @RequestMapping("/userList")
    public String userList(Model model){
        //1.查询业务层获取数据
        List<User> all = userService.findAll();
        //2.将数据保存到Model对象中返回给页面
        model.addAttribute("List", all);

        return "userList";
    }

    //新增
    @RequestMapping("/user/{name}/{age}/{sex}")
    public String userAdd(User user){

        userService.insert(user);
        return "redirect:/userList";
    }
    @RequestMapping("/user/{id}/{name}")
    public String userupdate(User user){

        userService.userUpdate(user);
        return "redirect:/userList";
    }
    @RequestMapping("/user/{id}")
    public String userDelete(User user){

        userService.userDelete(user);
        return "redirect:/userList";
    }
}
